//
//  AdoptPigViewController.m
//  PigMarket
//
//  Created by Felix on 2019/3/4.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "AdoptPigViewController.h"
#import "AdoptPigCell.h"
#import "AdoptHeaderView.h"
#import "SelectTribeViewController.h"
#import "DHGuidePageHUD.h"


const static CGFloat bannerHeight = 200;//轮播图高度
const static CGFloat headerHeight = 35 + 200;//轮播图高度 + 消息高度
@interface AdoptPigViewController ()<UITableViewDelegate,UITableViewDataSource>
/**
 headerView
 */
@property (nonatomic , strong) AdoptHeaderView *headerView;
/**
 tableView
 */
@property (nonatomic , strong) UITableView *myTableView;
/**
 bannerArr
 */
@property (nonatomic , strong) NSMutableArray *bannerArr;
/**
 varietyDataArr
 */
@property (nonatomic , strong) NSMutableArray *varietyDataArr;
/**
 memberID 判断是否登录
 */
@property (nonatomic , strong) NSNumber *memberId;
@end

@implementation AdoptPigViewController


#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置APP引导页
    if (![[NSUserDefaults standardUserDefaults] boolForKey:BOOLFORKEY]) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:BOOLFORKEY];
        // 静态引导页
           [self setStaticGuidePage];
        // 动态引导页
        //[self setDynamicGuidePage];
        // 视频引导页
        //[self setVideoGuidePage];
    }

    [self setUpView];
    
}


#pragma mark - init method

- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}


- (void)initView {
    [self.view addSubview:self.headerView];
    [self.view addSubview:self.myTableView];
}

- (void) initLayout {
    
    self.myTableView.frame = CGRectMake(0, self.headerView.bottom, KScreenWidth, KScreenHeight - self.headerView.bottom - TJ_TabBarHeight);
}

- (void) initAction {
    
}

- (void)initData {
    
    self.memberId = [UserInfoDataProvider sharedInstance].userInfo.memberId;
    [self requestBanner];
    [self requestMessage];
    [self requestPigVariety];
}

#pragma mark - delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return  0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AdoptPigCell *cell = [AdoptPigCell cellWithTableView:tableView];
    if (self.varietyDataArr.count > 0 && self.varietyDataArr.count > indexPath.row) {
        cell.varietyModel = self.varietyDataArr[indexPath.row];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath {

    if (self.varietyDataArr.count == 0) {
        [self toastWithContent:@"网络异常"];
        return;
    }
    [self pushToViewController:^{
        PigVariety *varietyModel = self.varietyDataArr[indexPath.row];
        SelectTribeViewController *selectTrideVC = [[SelectTribeViewController alloc] init];
        selectTrideVC.varietyId = varietyModel.varietiesId;
        [self.navigationController pushViewController:selectTrideVC animated:YES];
    }];
}
#pragma mark - target action

#pragma mark - private method
//banner请求
- (void)requestBanner {
    @weakify(self);
    [[[PigMarketApi sharedInstance] getAdoptBanner] subscribeNext:^(MessageList * _Nullable messageList) {
       @strongify(self);
        for (Message *model in messageList.list) {
            [self.bannerArr addObject:model.imgName];
        }
        
        self.headerView.cycleView.imageURLStringsGroup = self.bannerArr;
    } error:^(NSError * _Nullable error) {
        
    }];
}
//消息请求
- (void)requestMessage {
    
    NSNumber *memberId = nil;
    if ([JTool iSNull:self.memberId]) {
        memberId = @-1;
    }else {
        memberId = self.memberId;
    }
    @weakify(self);
    [[[PigMarketApi sharedInstance] adoptMessageWithMemberId:memberId] subscribeNext:^(MessageList * _Nullable messageList) {
        
    } error:^(NSError * _Nullable error) {
        @strongify(self);
        [self handlingError:error];
    }];
}
//品种请求
- (void)requestPigVariety {
    @weakify(self);
    [[[PigMarketApi sharedInstance] getPigVariety] subscribeNext:^(PigVarietyList * _Nullable pigVarietyList) {
        @strongify(self);
        [self.varietyDataArr addObjectsFromArray:pigVarietyList.list];
        [self.myTableView reloadData];
    } error:^(NSError * _Nullable error) {
        @strongify(self);
        [self handlingError:error];
    }];
}

#pragma mark - getter and setter

- (UITableView *)myTableView {
    
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        [_myTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        _myTableView.delegate = self;    
        _myTableView.dataSource = self;
        
        UIView *footer = [[UIView alloc] init];
        footer.backgroundColor = mBackgroudColor;
        footer.frame = CGRectMake(0, 0, KScreenWidth, 10);
        _myTableView.tableFooterView = footer;
    }
    return _myTableView;
}


- (AdoptHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[AdoptHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScaleHeight(200) + 35)];
        _headerView.backgroundColor = [UIColor whiteColor];
    }
    return _headerView;
}


- (NSMutableArray *)bannerArr {
    if (!_bannerArr) {
        _bannerArr = [[NSMutableArray alloc] init];
    }
    return _bannerArr;
}
- (NSMutableArray *)varietyDataArr {
    if (!_varietyDataArr) {
        _varietyDataArr = [[NSMutableArray alloc] init];
    }
    return _varietyDataArr;
}



#pragma mark - 设置APP静态图片引导页
- (void)setStaticGuidePage {
    //NSArray *imageNameArray = @[@"guideImage1.jpg",@"guideImage2.jpg",@"guideImage3.jpg",@"guideImage4.jpg",@"guideImage5.jpg"];
    NSArray *imageNameArray = @[@"guideImage1.png",@"guideImage2.png",@"guideImage3.png",@"guideImage4.png"];

    DHGuidePageHUD *guidePage = [[DHGuidePageHUD alloc] dh_initWithFrame:self.view.frame imageNameArray:imageNameArray buttonIsHidden:NO];
    guidePage.slideInto = YES;
    [self.navigationController.view addSubview:guidePage];
}

#pragma mark - 设置APP动态图片引导页
- (void)setDynamicGuidePage {
    NSArray *imageNameArray = @[@"guideImage6.gif",@"guideImage7.gif",@"guideImage8.gif"];
    DHGuidePageHUD *guidePage = [[DHGuidePageHUD alloc] dh_initWithFrame:self.view.frame imageNameArray:imageNameArray buttonIsHidden:YES];
    guidePage.slideInto = YES;
    [self.navigationController.view addSubview:guidePage];
}

#pragma mark - 设置APP视频引导页
- (void)setVideoGuidePage {
    NSURL *videoURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"guideMovie1" ofType:@"mov"]];
    DHGuidePageHUD *guidePage = [[DHGuidePageHUD alloc] dh_initWithFrame:self.view.bounds videoURL:videoURL];
    [self.navigationController.view addSubview:guidePage];
}


@end
